# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
触发中移动项目的接口
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

from datetime import datetime

import requests

from interfacetest.baseset.msydloan_api import API_MSYD
from interfacetest.baseset.test_logger import Logger
from msydwork.config.config_msydloan import w_url
from pycacho.cachobase.get_base_info import process

logger = Logger("post_msydloan").get_log()


headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
            'content-Type': 'application/json',
        }


usrIdCard = '650102198009251013'
mblNo = '13000000001'
usrIdName = '测测测'
hbUsrNo = 'HB650102198009251013'

# 借款成功后
brwOrdNo = '560202004010000006'
orgOrdNo = 'LW20200401HB000005'


def get_info(file):
    data = []
    with open(file, 'r',encoding= 'utf-8') as fp:
        for line in fp.readlines():
            line = line.lstrip().replace(' ','').replace('\n','')
            data.append(line)
    return(data)

def unfreeze():
    fu_url = w_url + '/cmpay/quota/v1/unfreeze'
    data = API_MSYD['unfreeze']
    data['brwOrdNo'] = brwOrdNo
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("资金方解冻额度接口响应信息：%s", data)

def mplCreditResultInform(qryCreditId):
    fu_url = w_url + '/cmpay/test/credit/notice'
    data = API_MSYD['mplCreditResultInform']
    data['hbUsrNo'] = hbUsrNo
    data['qryCreditId'] = qryCreditId
    print(data)
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("授信结果通知接口响应信息：%s", data)

def mplLoanResultInform():
    fu_url = w_url + '/cmpay/test/loan/notice'
    data = API_MSYD['mplLoanResultInform']
    print(data)
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("借款结果通知接口响应信息：%s", data)

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
内部逻辑：
1.根据借款订单号先去ms_loan查询是否存在借据；2.查询ms_repay_order中是否存在'N'、'M'、'P'状态记录
3.查询 ms_repayment_plan中还款计划；       4. 查询ms_plan对应期数的还款计划是否存在,没有就插入一条数据
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
def mplFundCharge():
    fu_url = w_url + '/spare/activeWithholdApply'      # /cmpay/test/foud/charge
    data = API_MSYD['mplFundCharge']
    data['brwOrdNo'] = brwOrdNo
    data['orgOrdNo'] = orgOrdNo
    data['rpyMod'] = '1'                # 0：还全部(购机不传),1-还某期,2-提前清贷,3-退货
    data['rpySeq'] = '1'                # 1-还某期的期数
    print(data)
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("资金方主动扣款接口响应信息：%s", data)

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
内部逻辑：查询结果为支付成功S时:
1.根据借款订单号先去ms_loan查询是否存在借据；2.根据借据ID查询ms_repayment_plan、ms_plan
3.插入一条数据到ms_plan，插入两条数据到ms_repay_hst；   4. 更新ms_plan和ms_loan，最后更新ms_repay_order
查询为处理中P不做任何处理；查询为失败F，直接更新ms_repay_order为失败
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
def mplFundChargeResultQry(rpyPreJrn):
    fu_url = w_url + '/cmpay/test/foud/result'
    data = API_MSYD['mplFundChargeResultQry']
    data['rpyPreJrn'] = rpyPreJrn
    print(data)
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("资金方主动扣款结果查询接口响应信息：%s", data)

def bath_apply(usrIdName,mblNo,usrIdCard,inCome, usrJob, schooling, maritalSta, cusSex, companyAddressCode, addressCode,usrProvNo,
               bankCardNo,bankCardName,bankMblNo,idCardFrontID,livePictureID):
    fu_url = w_url + '/cmpay/credit/v1/apply'
    jrnNo = '66' + usrIdCard
    qryCreditId = '026'+ usrIdCard
    hbUsrNo = '99' + usrIdCard
    data = API_MSYD['apply']
    data['jrnNo'] = jrnNo
    data['qryCreditId'] = qryCreditId
    data['hbUsrNo'] = hbUsrNo
    data['usrIdName'] = usrIdName
    data['mblNo'] = mblNo
    data['usrIdCard'] = usrIdCard
    data['inCome'] = inCome
    data['usrJob'] = usrJob
    data['schooling'] = schooling
    data['maritalSta'] = maritalSta
    data['cusSex'] = cusSex
    data['companyAddressCode'] = companyAddressCode
    data['addressCode'] = addressCode
    data['usrProvNo'] = usrProvNo
    data['bankCardNo'] = bankCardNo
    data['bankCardName'] = bankCardName
    data['bankMblNo'] = bankMblNo
    data['idCardFrontID'] = idCardFrontID
    data['livePictureID'] = livePictureID
    print(data)
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("授信申请接口响应信息：%s", data)

def file_apply():
    info = get_info('..\\test\\test-credit.txt')
    for data in info:
        name = data.split('|')[0]
        phone = data.split('|')[1]
        idCard = data.split('|')[2]
        inCome = data.split('|')[3]
        usrJob = data.split('|')[4]
        schooling = data.split('|')[5]
        maritalSta = data.split('|')[6]
        cusSex = data.split('|')[7]
        companyAddressCode = data.split('|')[8]
        addressCode = data.split('|')[9]
        usrProvNo = data.split('|')[10]
        bankCardNo = data.split('|')[11]
        bankCardName = data.split('|')[12]
        bankMblNo = data.split('|')[13]
        idCardFrontID = data.split('|')[14]
        livePictureID = data.split('|')[15]
        print(name, phone, idCard, inCome, usrJob, schooling, maritalSta, cusSex, companyAddressCode, addressCode,usrProvNo)
        bath_apply(name, phone, idCard, inCome, usrJob, schooling, maritalSta, cusSex, companyAddressCode, addressCode,usrProvNo,
                   bankCardNo,bankCardName,bankMblNo,idCardFrontID,livePictureID)

def main():
    info = process(5)
    for i in range(len(info)):
        qryCreditId = info[i][0]
        usrIdName = info[i][1]
        usrIdCard = info[i][2]
        mblNo = info[i][3]
        hbUsrNo = '99' + usrIdCard
        # 借款成功后
        orgOrdNo = info[i][4]
        brwOrdNo = info[i][5]
        print(qryCreditId,usrIdName,usrIdCard,mblNo,hbUsrNo,orgOrdNo,brwOrdNo)

if __name__ == '__main__':
    q = 0
    if q == 0:
        file_apply()
    elif q == 6:
        unfreeze()
    elif q == 11:
        mplFundCharge()                # 资金方主动扣款接口
    elif q == 12:
        qryCreditId = '190731550003060005'
        mplCreditResultInform(qryCreditId)     #  扣款结果通知接口
    elif q == 13:
        mplLoanResultInform()                  # 借款结果通知接口
    elif q == 14:
        rpyPreJrn = '190805550003101621'
        mplFundChargeResultQry(rpyPreJrn)      # 资金方主动扣款结果查询接口